﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DAO;
using BUS;
using System.Xml;

namespace WebRaoVat
{
    public partial class ChiTietTinRao : System.Web.UI.Page
    {
        public NguoiDungDTO NguoiDungHienTai = new NguoiDungDTO();
        public string DiaDiem = "";

        public string tenDanhMucChinh = "";
        public int maDanhMucChinh;

        public string tenDanhMucCon = "";
        public int maDanhMucCon;
        public int maTinRaoVat = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Session["TrangHienTai"] = "ChiTietTinRao.aspx?MaTin=" + Request.QueryString["MaTin"].ToString();
                maTinRaoVat = int.Parse(Request.QueryString["MaTin"].ToString());
            }
            catch (Exception)
            {
            }

            if (-1 == maTinRaoVat)
                return;

            NguoiDungHienTai = NguoiDungBUS.getAlongCode((TinRaoVatBUS.getAlongCode_MaTinRaoVat(int.Parse(Request.QueryString["MaTin"].ToString()))).MaNguoiDung);
            DiaDiem = DiaDiemBUS.getAlongCode((TinRaoVatBUS.getAlongCode_MaTinRaoVat(int.Parse(Request.QueryString["MaTin"].ToString()))).MaDiaDiem).TenDiaDiem;
            Session["NguoiDangTinDuocXem"] = NguoiDungHienTai.TenNguoiDung;

            //Tăng số lần xem
            TinRaoVatDTO CapNhat = TinRaoVatBUS.getAlongCode_MaTinRaoVat(int.Parse(Request.QueryString["MaTin"].ToString()));
            CapNhat.SoLanXem += 1;
            TinRaoVatBUS.Update(CapNhat);

            if (!IsPostBack)
            {
                ddlBaoViPham.Items.Add("Bài viết");
                ddlBaoViPham.Items.Add("Thành viên đăng bài");
            }


            maDanhMucCon = TinRaoVatBUS.getMaDanhMucCon(maTinRaoVat);
            tenDanhMucCon = DanhMucConBUS.getTenDanhMuc(maDanhMucCon);

            maDanhMucChinh = DanhMucConBUS.getMaDanhMucChinh(maDanhMucCon);
            tenDanhMucChinh = DanhMucChinhBUS.getTen(maDanhMucChinh);

        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Page.Theme = ((XmlElement)XL_XML.ReadNodeSingle(XL_XML.ReadDocumentFile(Server.MapPath("Gui.xml")),
            "/GIAODIEN/THEME")).GetAttribute("ten");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["NguoiDungDangDangNhap"] == null || Session["NguoiDungDangDangNhap"].ToString().Length <= 0)
                {
                    return;
                }

                NguoiDungDTO nguoiDungHienTai = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString());

                if (maTinRaoVat == -1)
                    return;

                BaiTraLoiDTO baiviet = new BaiTraLoiDTO();
                int matin = 1;

                try
                {
                    matin = int.Parse(Request.QueryString["MaTin"].ToString());

                }
                catch (Exception)
                {
                    return;
                }
                
                baiviet.MaTinRaoVat = matin;                
                baiviet.NoiDungTraLoi = "<p>" + CKEditorControl1.Text + "</p>";
                baiviet.ThoiGianTraLoi = DateTime.Now;
                baiviet.MaNguoiDung = nguoiDungHienTai.MaNguoiDung;
                baiviet.TraLoiCho = 1;

                BaiTraLoiBUS.Insert(baiviet);
                TinRaoVatBUS.capNhatThoiGianUp(matin, DateTime.Now);
                DataList1.DataBind();

                CKEditorControl1.Text = "";
            }
            catch (System.Exception ex)
            {
                ShowMessage("Phản hồi không thành công !");
            }            
        }

        protected void lbtnNguoiDang_Click(object sender, EventArgs e)
        {
            Session["LoaiXemNguoiDung"] = "2";
            Response.Redirect("NguoiDung.aspx?MaNguoiDung=" + NguoiDungHienTai.MaNguoiDung.ToString());
        }

        private void ShowMessage(string mess)
        {
            string strBuilder = "<script language='javascript'>alert('" + mess + "')</script>";
            Response.Write(strBuilder);
        }

        public List<string> ListMessage = new List<string>();

        protected void btnGoi_Click(object sender, EventArgs e)
        {
            ListMessage.Clear();
            string LyDo = txtLyDo.Text.Trim();

            if (LyDo.Length <= 0)
            {
                ListMessage.Add("Bạn chưa nhập lý do");
                return;
            }

            if (ddlBaoViPham.SelectedIndex == 0)
            {
                LichSuTinRaoVatViPhamDTO ls = new LichSuTinRaoVatViPhamDTO();

                try
                {
                    ls.MaTinRaoVatViPham = int.Parse(Request.QueryString["MaTin"].ToString());
                    ls.MaNguoiDungBaoCaoViPham = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString()).MaNguoiDung;
                    ls.ThoiGianBaoCaoViPham = DateTime.Now;
                    ls.LyDo = LyDo;
                    ls.Deleted = 0;

                    LichSuTinRaoVatViPhamBUS.Insert(ls);
                    ListMessage.Add("Gởi thành công !");
                }
                catch (System.Exception ex)
                {
                    ListMessage.Add("Gởi không thành công !");
                }
            }
            else
            {
                LichSuNguoiDungViPhamDTO ls = new LichSuNguoiDungViPhamDTO();

                try
                {
                    ls.MaNguoiDungViPham = NguoiDungHienTai.MaNguoiDung;
                    ls.MaNguoiDungBaoCaoViPham = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString()).MaNguoiDung;
                    ls.ThoiGianBaoCaoViPham = DateTime.Now;
                    ls.Deleted = 0;
                    ls.ThoiGianCanhCao = 30;
                    ls.LyDo = LyDo;

                    LichSuNguoiDungViPhamBUS.Insert(ls);
                    ListMessage.Add("Gởi thành công !");
                }
                catch (System.Exception ex)
                {
                    ListMessage.Add("Gởi không thành công !");
                }
            }
        }
    }
}
